import { BasicColumn, FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Switch, TableColumnType } from 'ant-design-vue';
import { enabledPermissionStateById } from '/@/api/system/system';
import { useMessage } from '/@/hooks/web/useMessage';
import { PermissionReq, PermissionResp } from '/@/api/system/modal/permissionModel';

export const columns: BasicColumn[] = [
  {
    title: '权限名称',
    dataIndex: 'permissionName',
    width: 200,
  },
  {
    title: '权限编码',
    dataIndex: 'permissionCode',
    width: 180,
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: 50,
  },
  {
    title: '状态',
    dataIndex: 'stateId',
    width: 120,
    customRender: ({ record }) => {
      if (!Reflect.has(record, 'stateId')) {
        record.pendingStatus = false;
      }
      return h(Switch, {
        checked: record.stateId === 1,
        checkedChildren: '已启用',
        unCheckedChildren: '已禁用',
        loading: record.pendingStatus,
        onChange(checked: boolean) {
          record.pendingStatus = true;
          const newStateId = checked ? 1 : 0;
          const permissionReq: PermissionReq = {
            id: record.id,
            stateId: newStateId,
          };
          const { createMessage } = useMessage();
          enabledPermissionStateById(permissionReq)
            .then(() => {
              record.stateId = newStateId;
              createMessage.success(record.stateId === 1 ? '权限已启用' : '权限已禁用');
            })
            .catch((error: any) => {
              const { response, message } = error || {};
              const msg: string = response?.data?.message ?? message;
              createMessage.error(msg);
            })
            .finally(() => {
              record.pendingStatus = false;
            });
        },
      });
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createdDate',
    width: 180,
  },
  {
    title: '备注',
    dataIndex: 'note',
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'param.permissionNameByLike',
    label: '权限名称',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'param.permissionCodeByLike',
    label: '权限编码',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    field: 'param.stateId',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 0 },
      ],
    },
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'permissionName',
    label: '权限名称',
    required: true,
    component: 'Input',
  },
  {
    field: 'permissionCode',
    label: '权限编码',
    required: true,
    component: 'Input',
  },
  {
    field: 'sort',
    label: '排序',
    required: true,
    component: 'InputNumber',
  },
  {
    field: 'stateId',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '启用', value: 1 },
        { label: '停用', value: 0 },
      ],
    },
  },
  {
    label: '备注',
    field: 'note',
    component: 'InputTextArea',
    componentProps: {
      rows: 4,
      maxlength: 100,
      showCount: true,
    },
  },
];

export const componentColumns: TableColumnType<PermissionResp>[] = [
  {
    title: '序号',
    dataIndex: 'serialNumber',
    width: '10%',
  },
  {
    title: '组件名称',
    dataIndex: 'componentTitle',
    width: '40%',
    ellipsis: true,
  },
  {
    title: '组件编码',
    dataIndex: 'componentCode',
    width: '40%',
    ellipsis: true,
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: '10%',
  },
];

export const apiColumns: TableColumnType<PermissionResp>[] = [
  {
    title: '序号',
    dataIndex: 'serialNumber',
    width: '10%',
  },
  {
    title: '接口名称',
    dataIndex: 'apiTitle',
    width: '40%',
    ellipsis: true,
  },
  {
    title: '接口编码',
    dataIndex: 'apiCode',
    width: '40%',
    ellipsis: true,
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: '10%',
  },
];
